﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

//add system.drawing reference
using System.Drawing;
using System.Drawing.Imaging;


//don't forget to add refernces to the DLLs
using AForge.Video;
using AForge.Video.DirectShow;

namespace ConsoleApplication1
{
    class Program
    {
        public static FilterInfoCollection videoDevices;
        public static VideoCaptureDevice videoDevice;

        static void Main(string[] args)
        {
            //list of video devices
            videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);

            //one device
            videoDevice = new VideoCaptureDevice(videoDevices[0].MonikerString);
            
            //attach (callback) on new frame
            videoDevice.NewFrame += new NewFrameEventHandler(new_frame);
            videoDevice.Start();

        }

        //when new frame arrives
        static private void new_frame(object sender, NewFrameEventArgs eventArgs)
        {
            Bitmap a = (Bitmap)eventArgs.Frame.Clone();
            saveBMPtoJpeg(a, "C:\\temp\\pic100.jpeg",100);
            saveBMPtoJpeg(a, "C:\\temp\\pic80.jpeg", 80);
            saveBMPtoJpeg(a, "C:\\temp\\pic60.jpeg", 60);
            saveBMPtoJpeg(a, "C:\\temp\\pic40.jpeg", 40);
            saveBMPtoJpeg(a, "C:\\temp\\pic20.jpeg", 20);
            saveBMPtoJpeg(a, "C:\\temp\\pic5.jpeg", 5);

        }

        private static void saveBMPtoJpeg(Bitmap image, string location, long quality)
        {

            //Image.FromHbitmap(a.GetHbitmap()).Save("C:\\Users\\Icarus\\Desktop\\pic.jpeg",ImageFormat.Jpeg,);

            //code below and function GetEncoder straight from http://msdn.microsoft.com/en-us/library/system.drawing.imaging.encoderparameter.aspx
            ImageCodecInfo jgpEncoder = GetEncoder(ImageFormat.Jpeg);

            // Create an Encoder object based on the GUID 
            // for the Quality parameter category.
            System.Drawing.Imaging.Encoder myEncoder =
                System.Drawing.Imaging.Encoder.Quality;

            // Create an EncoderParameters object. 
            // An EncoderParameters object has an array of EncoderParameter 
            // objects. In this case, there is only one 
            // EncoderParameter object in the array.
            EncoderParameters myEncoderParameters = new EncoderParameters(1);

            EncoderParameter myEncoderParameter = new EncoderParameter(myEncoder, quality);
            myEncoderParameters.Param[0] = myEncoderParameter;
            image.Save(location, jgpEncoder, myEncoderParameters);
        }

        private static ImageCodecInfo GetEncoder(ImageFormat format)
        {

            ImageCodecInfo[] codecs = ImageCodecInfo.GetImageDecoders();

            foreach (ImageCodecInfo codec in codecs)
            {
                if (codec.FormatID == format.Guid)
                {
                    return codec;
                }
            }
            return null;
        }
    }
}
